#!/bin/bash

#kill "$jarName" pid
#jar包存放位置
jarPath='/usr/local/toolTest'

#jar包名称
# shellcheck disable=SC2034
jarName='etm-tool-pos-1.0.0.jar'

#jar包备份地址位置
bankPath=/usr/local/toolTest/bank/$(date +"%Y‐%m‐%d-%H:%M:%S")

# shellcheck disable=SC2009
pidList=$(ps -ef | grep "$jarName" | grep -v "grep" | awk '{print $2}') #找到tomcat的PID号

if [ "$pidList" ]; then
  echo "jar Id list :$pidList" #显示pid

  kill -9 "$pidList" #杀掉改进程

  echo "KILL $pidList:" #提示进程以及被杀掉

  echo "jar stop success"
else
  echo "项目未启动"
fi

echo "开始备份旧安装包"

cd "${jarPath}" || exit

if [ -f "$jarName" ]; then
  echo "文件存在"
  echo "创建目录 $bankPath:"
  mkdir "$bankPath"
  echo "备份文件"
  mv -f "$jarPath"/"$jarName" "$bankPath"
else
  echo "备份失败-文件不存在"
fi

cd ${jarPath}/预备 || exit

if [ -f "$jarName" ]; then
  echo "移动最新包"
  # shellcheck disable=SC1078
  mv -f "$jarPath"/预备/"$jarName" "$jarPath"
else
  echo "文件不存在-不处理"
fi

cd $jarPath || exit
# shellcheck disable=SC2188
>nohup.out

if [ -f etm-tool-pos-1.0.0.jar ]; then
  echo "启动服务"
  nohup java -Xms512m -Xmx512m -XX:MaxMetaspaceSize=512m -jar "$jarPath"/"$jarName" &
else
  echo "文件不存在-启动服务失败"
fi
